package top.szhome.develop.generator;

import top.szhome.develop.entity.Template;
import top.szhome.framework.starter.template.engine.TemplateEngine;
import top.szhome.framework.starter.template.engine.TemplateEngineFactory;
import top.szhome.framework.starter.template.engine.TemplateEngineType;

import java.io.File;
import java.io.IOException;

/**
 * 代码生成器
 *
 * @author 唐加利
 * @date 2023/9/2
 **/
public class Generator {

    /**
     * 代码生成
     * @param template
     * @param context
     * @return
     * @throws IOException
     */
    public static String getCode(Template template, Context context) {
        TemplateEngineType engineType = TemplateEngineType.valueOf(template.getEngine().toUpperCase());
        TemplateEngine engine = TemplateEngineFactory.get(engineType);
        String templateName = getTemplateName(template.getName(), template.getId());
        return engine.process(templateName, template.getTemplate(), context);
    }

    /**
     * 获取文件
     *
     * 源码文件：模板根路径 / src/main/java / 工程基础包路径 / 模板父路径 / 模板文件名
     * 资源文件：模板根路径 / src/main/resources / 模板父路径 / 模板文件名
     * 其它文件：模板根路径 / 模型模块编码 / 模板父路径 / 模板文件名
     * @param template
     * @param context
     * @return
     */
    public static String getFile(Template template, Context context) {
        TemplateEngineType engineType = TemplateEngineType.valueOf(template.getEngine().toUpperCase());
        TemplateEngine engine = TemplateEngineFactory.get(engineType);
        String templateName = getTemplateName(template.getName(), template.getId());
        String rootPath = engine.process(getTemplateName(templateName, "root-path"), template.getRootPath(), context);
        String parentPath = engine.process(getTemplateName(templateName, "parent-path"), template.getParentPath(), context);
        String file = engine.process(getTemplateName(templateName, "file"), template.getFile(), context);
        String pkgPath = context.getPackage().replaceAll("\\.", File.separator);

        String path = context.getProject().getCode() + File.separator;
        switch (template.getFileType()) {
            case "source":
                path = path + rootPath + "/src/main/java/" + pkgPath + File.separator + parentPath + File.separator + file;
                break;
            case "resource":
                path = path + rootPath + "/src/main/resources/" + parentPath + File.separator + file;
                break;
            default:
                path = path + rootPath + File.separator + parentPath + File.separator + file;
        }

        return path;
    }

    /**
     * 获取模板名称
     * @param parts
     * @return
     */
    private static String getTemplateName(Object... parts){
        StringBuilder builder = new StringBuilder();
        for(int i = 0; i < parts.length; i++){
            Object part = parts[i];
            if(i != 0){
                builder.append("-");
            }
            builder.append(part.toString());
        }
        return builder.toString();
    }

}
